package vg.services.log_manager;

import vg.services.log_manager.interfaces.ILogger;
import vg.services.log_manager.interfaces.ILogManager;
import vg.services.log_manager.interfaces.IWindowMessage;
import vg.services.log_manager.realization.ConsoleLogger;
import vg.services.log_manager.realization.FileLogger;
import vg.services.log_manager.realization.WindowMessage;

/**
 * Log manager.
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class LogManager implements ILogManager {
	// Mutex
	private final Object generalMutex = new Object();
	
	// Main components
	private ILogger logger;
	private IWindowMessage windowMessage;
	
	public LogManager() {
		try {
			logger = new FileLogger();
		} catch (Exception ex) {
			logger = new ConsoleLogger();
		}
		windowMessage = new WindowMessage();
	}
	
	@Override
	public ILogger getLogger() {
		synchronized (generalMutex) {
			return logger;
		}
	}
	
	@Override
	public IWindowMessage getWindowMessage() {
		synchronized (generalMutex) {
			return windowMessage;
		}
	}
	
	@Override
	public boolean setLogger(ILogger newLogger) {
		synchronized (generalMutex) {
			if (newLogger != null) {
				logger = newLogger;
				return true;
			}
		}
		return false;
	}
	
	@Override
	public boolean setWindowMessage(IWindowMessage newWindowMessage) {
		synchronized (generalMutex) {
			if (newWindowMessage != null) {
				windowMessage = newWindowMessage;
				return true;
			}
		}
		return false;
	}
}
